<?php
$cs = Yii::app()->getClientScript();
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_find_1.css');
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_list.css');
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$wxControl = $screen->getChildControl('BUIWXControl_1');
$venderList = Blyn::app()->getActiveApp()->findVenderListByEventType();
$courtTypeList = Blyn::app()->getActiveApp()->getCourtTypeList();
    if(isset($nowCity)) {
        $city = $nowCity;//通常是省市拼音，如：china.chongqing
    }else {
        $attribute = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId());
        // if(isset($attribute))
        // {
        //   $city = $attribute->getValue();
        // }
        // else {
            $city='china.chongqing';
        // }
    }
$treeTable = new BTreeTable('BWInputValue');
$itemData = array(
    'city' => $city,
    'bookingStatus' => 'available',
    //下面的两个条件非常重要：
    'bookingStartTime' => '<='.time(),
    'bookingEndTime' => '>='.time(),
    //only get item for current space
    'spaceId'=>  Blyn::app()->getActiveSpace()->getId()
);
$app = Blyn::app()->getActiveApp();
$venderCourtTypeBookableList = array();

if (isset($_GET["openid"])) {
     $jq_openid=$_GET["openid"];
     $user = blyn::app()->getCurrentUser();
     $user->addContextVariable('jq_openid',$jq_openid);
}
?>

<meta name="viewport" contdent="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script>
//$("document").ready(function() {
//    $(".booking_list > dd").click(function() {
//        $(this).css("backgroundColor","rgb(243, 243, 246)");
//        $(this).siblings().css("backgroundColor","white");
//    });
//}); 
</script>
<body id="index">

    <header class="navbar">
        <div class="nav-wrap-left">
            <?php
            $bookingFindUrl = $wxControl->getLinkUrl(array(
                'name' => 'booking_find_1',
                'id' => 'booking_find_1'),array(
                'nowCity' => $city,   //$city=重庆
                'findType' => 'full' 
            ));
            ?>
            <a class="react brand" href="<?php echo $bookingFindUrl;?>">
                <span class="logo">
                    <font style="font-size:20px; font-family: Microsoft YaHei;">场地预订</font>
                </span>
            </a>
            <?php 
            $changeCityUrl = $wxControl->getLinkUrl(array(
                            'name' => '修改城市',
                            'id' => 'booking_change_city')
                    );
            ?>
            <span class="nav-city"><a gaevent="imt/hd/cityBottom" href="<?php echo $changeCityUrl;?>" class="react">
                <?php
                $cityList = Blyn::app()->getActiveApp()->getCityList();
                echo $cityList[$city];
                ?>
                <space></space>
                <i class="text-icon icon-downarrow"></i></a>
            </span>

<!--            <div >
                <?php // echo CHtml::beginForm() ?>
                <?php
                $screen = Blyn::app()->getUIManager()->getCurrentScreen();
                $cityList = Blyn::app()->getActiveApp()->getCityList();
                $model = new BDynamicFormModel();
                $model->addField('findText');
                $model->addField('userCity');
                //by default, set user city to ChongQing
                $model->userCity = 'china.chongqing';

                $savedUserCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId());
                if ($savedUserCity instanceof BAttribute) {
                    $model->userCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId())->getValue();
                }

                if ($_POST != NULL && isset($_POST['BDynamicFormModel'])) {
                    $model->attributes = $_POST['BDynamicFormModel'];
                }

                if (!$savedUserCity instanceof BAttribute || $savedUserCity->getValue() != $model->userCity) {
                    Blyn::app()->getCurrentUser()->addAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId(), $model->userCity);
                }
                ?>
                <div >
                    <div >
                        <?php // echo CHtml::activeDropDownList($model, 'userCity', $cityList, array('class' => 'span2')); ?>
                    </div>
                </div>
             
                <?php // echo CHtml::endForm() ?>
            </div>     -->
     </div>
        
        <!--刷新按钮-->
        <a class="refresh" href="<?php echo $bookingFindUrl;?>">
            <img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/refresh.png';?>" width="20" height="20"/>
        </a>
        <div id="ajax-loaderdiv" style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 35000;background-color: rgba(0, 0, 0, 0.3);display: none;">
            <img src="/billyn-dev/images/ajax-loader.gif" class="ajax-loader"style="position: absolute;left: 50%;top: 50%;margin-left: -16px;margin-top: -16px;display: block;"/>
        </div>
        <script>
        $(".refresh").click(function() {
            $("#ajax-loaderdiv").show();
        });
        function goRecommend(elem,link) {
            elem.style.backgroundColor = "rgb(243, 243, 246)";
            location.href = link;
        }
        </script>
        <?php
        $wxControl->renderView('booking_navigation',array());?>
    </header>
    <dl style="margin:0;">
        <dd style="margin:0;height:70px;overflow:hidden;background-color: #88C25E;">
            <table style="width:100%;text-align: center;">
                <tr>
                    <td style="overflow: hidden;"><img src="<?php echo Yii::app()->request->baseUrl . '/images/mobile/let_go.png'?>" /></td>
                    <td style="overflow: hidden;"><img src="<?php echo Yii::app()->request->baseUrl . '/images/mobile/balls.png'?>" width="140" height="70" /></td>
                </tr>
            </table>
        </dd>
    </dl>
    <dl class="courtType_list">
        <dd class="courtType">
            <div style="width:100%;height:8px;background-image:url(<?php echo Yii::app()->request->baseUrl . '/images/mobile/green_line.png'?>);"></div>
            <?php 
//            $eventList = $app->findEventList();
//            foreach ($eventList as $event) {
//                $vender = $event->getMessageManager()->getMessage('vender');
//                $bookableList = $vender->getMessageManager()->getMessageList();
                $courtTypeIcons = array(
                    'soccer_court_5' => Yii::app()->request->baseUrl . '/images/mobile/3.png',
                    'soccer_court_7' => Yii::app()->request->baseUrl . '/images/mobile/4.png',
                    'soccer_court_11' => Yii::app()->request->baseUrl . '/images/mobile/2.png',
                    'badminton_court' => Yii::app()->request->baseUrl . '/images/mobile/1.png',
                );
                if(isset($findContext)) {
                    $findText = '';
                    $app->findByFindText($findText, $findContext);
                }
            ?>

            <ul style=" background-color: #60BF60; ">
                    <?php  
                    $valueList = $treeTable->getValueList('courtType', array('itemData' => $itemData));
                    foreach ($courtTypeList as $courtType => $courtName) {  
                        $findText = 'city=' . $city . ' courtType=' . $courtType;
                        $flagText = $courtName;
                        $count = isset($valueList[$courtType]) ? $valueList[$courtType] : 0;
                        $courtUrl = $wxControl->getLinkUrl(array(
                            'name' => $courtName,
                            'id' => 'booking_list'), array(
                            'listPage' => 'booking_list_by_day',
                            'findText' => $findText,
                            'flagText' => $flagText,
                            'nowCity' => $city  //通常是省市拼音，如：china.chongqing
            //                            'findContext' => serialize(array('itemMatch' => '='))
                            ))
                        ?>
                        <li  style="width:49.5%; border-right: 1px solid #dedede;border-bottom: 1px solid #dedede;float:left;text-align: center;padding:15px 0;">
                            <a class="react" href="<?php echo $courtUrl;?>">
                            &nbsp;&nbsp;&nbsp;
                            <img src="<?php echo $courtTypeIcons[$courtType]; ?>" style="height:50px;width:50px;">
                            <em  style="font-size: 12px; margin-top: 10px;"class="num_court"><?php echo $count; ?></em>
                            <p style="font-size: small;text-align: inherit;line-height: 30px;"><?php echo $flagText;?></p>
                            </a>
                        </li>
                    <?php }?>
                </ul>
            <div style="width: 100%;height: 0.1px;clear: both;"></div>
            <?php // }?>
      </dd>
    </dl>
    
    
    <div style="padding-left: 10px;"><!--class=more-->
        <img src="<?php echo Yii::app()->request->baseUrl . '/images/mobile/fire_ball.png'?>" />
    </div>
        <style>
            .recommend td {
                border-color: #dedede;border-style: solid;
            }
            .recommend .timeSlot {
                width: 25%;color:rgb(255, 112, 0);font-weight: bold;border: 1px solid #dedede;
            }
            .recommend .court_type {
                line-height: 40px;color:gray;/*padding: 5px 0;*/border-width: 1px 0;cursor: pointer;
            }
            .recommend .leave_count {
                line-height: 40px;color: #2bb2a3;border-width: 1px 0;cursor: pointer;
            }
            .recommend .leave_count font {
                font-family: fantasy;font-size: x-large;
            }
        </style> 
        <?php
        //设置黄金时间段
        $setStartOfTimeSlot = '>=170000';
        $setEndOfTimeSlot = '<=210000';
        $kingCourtListArr = array();

        foreach ($courtTypeList as $courtType => $courtTypeZn) {
            $valueList1 = $treeTable->getValueList('startTime', array(
                'itemData' => array_merge($itemData,array(
                'startOfTimeSlot' => $setStartOfTimeSlot,
                'endOfTimeSlot' => $setEndOfTimeSlot,
                'startDay' => '<=5',
                'courtType' => $courtType
                    ))
                ));
            $valueList2 = $treeTable->getValueList('endTime', array(
                'itemData' => array_merge($itemData,array(
                'startOfTimeSlot' => $setStartOfTimeSlot,
                'endOfTimeSlot' => $setEndOfTimeSlot,
                'startDay' => '<=5',
                'courtType' => $courtType
                    ))
                ));
            reset($valueList2);
            foreach ($valueList1 as $dateStamp => $count) {
                $week = date('l',$dateStamp);
                $startOfTimeSlot = date('H:i',$dateStamp);
                $endOfTimeSlot = date('H:i',key($valueList2));

                //筛选出周1-周五的数据
                if(in_array($week,array('Monday', 'Tuesday' ,'Wednesday','Thursday','Friday'))) {
                    $week = 'weekday';
                    $isInValueArr = false;
                    foreach ($kingCourtListArr as $key => $valueArr) {
                        if($valueArr['startOfTimeSlot']==$startOfTimeSlot && $valueArr['endOfTimeSlot']==$endOfTimeSlot && $valueArr['dayType']==$week) {
                            $kingCourtListArr[$key]['courtType'][$courtTypeZn] = $valueArr['courtType'][$courtTypeZn] + $count;
                            $isInValueArr = true;
                            break;
                        }
                    }
                    if(!$isInValueArr) {
                        /*添加一条数据，如：$kingCourtListArr = array (
                                                ‘0’=>array(‘startTime’=>’17:00’,’endTime’=>’18:00’,"dayType"=> "weekday",’courtType’=>array(
                                                        '11人制足球场地' => 0,
                                                        '7人制足球场地' => 0,
                                                        '5人制足球场地' => 0,
                                                        '羽毛球场地' => 0）
                                            )),*/
                        $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> 'weekday','courtType'=>array());
                        foreach ($courtTypeList as $key => $value) {
                            $kingCourtListArr[key($kingCourtListArr)]['courtType'][$value] = 0;
                        }
                        $kingCourtListArr[key($kingCourtListArr)]['courtType'][$courtTypeZn] = intval($count);
                    }
                }
                next($valueList2);
            }
        
        

            $valueList3 = $treeTable->getValueList('startTime', array('itemData' => array_merge($itemData,array('startDay' => '>=6','courtType' => $courtType))));
            $valueList4 = $treeTable->getValueList('endTime', array('itemData' => array_merge($itemData,array('startDay' => '>=6','courtType' => $courtType))));
            reset($valueList4);
            foreach ($valueList3 as $dateStamp => $count) {
                $week = date('l',$dateStamp);

                if(in_array($week,array('Saturday', 'Sunday'))) {
//                    $week = 'weekend';
                    $isInValueArr = false;
                    foreach ($kingCourtListArr as $key => $valueArr) {
                        if($valueArr['dayType']==$week) {
                            $kingCourtListArr[$key]['courtType'][$courtTypeZn] = $valueArr['courtType'][$courtTypeZn] + $count;
                            $isInValueArr = true;
                            break;
                        }
                    }
                    if(!$isInValueArr) {
                        $startOfTimeSlot = strtotime(date('Y-m-d',$dateStamp).' 00:00:00');
                        $endOfTimeSlot = $startOfTimeSlot + 60 * 60 * 24;
                        $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> $week,'courtType'=>array());
                        foreach ($courtTypeList as $key => $value) {
                            $kingCourtListArr[key($kingCourtListArr)]['courtType'][$value] = 0;
                        }
                        $kingCourtListArr[key($kingCourtListArr)]['courtType'][$courtTypeZn] = intval($count);
                    }
                }
                next($valueList4);
            }
        }
        ?>
    
        <?php 
        unset($itemData['bookingStatus']);
        unset($itemData['bookingStartTime']);
        unset($itemData['bookingEndTime']);
        ?>
    <dl class="booking_list" style="background-color:white;">
        <dd>
            <br/>
            <p style="color:green;font-weight:bold;padding-left: 10px;">工作日</p>
            <br/>
            <?php
            //循环遍历数组
            foreach ($kingCourtListArr as $key => $valueArr) {
                if($valueArr['dayType'] == 'weekday') {
                //通过设置设置startTime和endTime把时间限定在某一天
                $startOfTimeSlot = $valueArr['startOfTimeSlot'];
                $endOfTimeSlot = $valueArr['endOfTimeSlot'];
                $findText = '';
                foreach ($itemData as $key => $value) {
                    $firstChar = substr($value, 0, 1);
                    if($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') {
                        $findText = ($findText ? $findText." ".$key.$value : $key.$value);
                    }else {
                        $findText = ($findText ? $findText." ".$key."=".$value : $key."=".$value);
                    }
                }
                $findText = $findText." startOfTimeSlot=". str_replace(':00', '0000', $startOfTimeSlot)." endOfTimeSlot=". str_replace(':00', '0000', $endOfTimeSlot);

                $flagText = '工作日';
                $findText = $findText.' startDay<=5';
                    ?>
                        <table class="recommend" style="width:100%;text-align: center">
                            <tbody>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_11','flagText' => $flagText,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <td rowspan="4" class="timeSlot"><?php echo $startOfTimeSlot;?><p style="text-align:center;line-height:10px;">-</p><?php echo $endOfTimeSlot;?></td>
                                <?php if($valueArr['courtType']['11人制足球场地'] != 0) {?>
                                <td class="court_type">11人制足球场地</td>
                                <?php // echo $findText.' courtType=soccer_court_11';?>
                                <td class="leave_count">剩余场地：<font><?php echo $valueArr['courtType']['11人制足球场地']?></font></td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl =  $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_7','flagText' => $flagText,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['7人制足球场地'] != 0) {?>
                                <td class="court_type">7人制足球场地</td>
                                <td class="leave_count">
                                        剩余场地：<font><?php echo $valueArr['courtType']['7人制足球场地']?></font>
                                    </td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_5','flagText' => $flagText,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['5人制足球场地'] != 0) {?>
                                <td class="court_type">5人制足球场地</td>
                                <td class="leave_count">
                                    剩余场地：<font><?php echo $valueArr['courtType']['5人制足球场地']?></font>
                                    </td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=badminton_court','flagText' => $flagText,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['羽毛球场地'] != 0) {?>
                                <td class="court_type">羽毛球场地</td>
                                <td class="leave_count">
                                    剩余场地：<font><?php echo $valueArr['courtType']['羽毛球场地']?></font>
                                    </td>
                                <?php }?>
                            </tr>
                            </tbody>
                        </table>
                <?php 
                    }
                }
            ?>   
        </dd>
    </dl>
          
    <dl class="booking_list" style="background-color:white;">
        <dd>
            <br/>
            <p style="color:green;font-weight:bold;padding-left: 10px;">周末</p>
            <br/>
            <?php
            //循环遍历数组
            foreach ($kingCourtListArr as $key => $valueArr) {
                //通过设置设置startTime和endTime把时间限定在某一天
                $findText = '';
                foreach ($itemData as $key => $value) {
                    $firstChar = substr($value, 0, 1);
                    if($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') {
                        $findText = ($findText ? $findText." ".$key.$value : $key.$value);
                    }else {
                        $findText = ($findText ? $findText." ".$key."=".$value : $key."=".$value);
                    }
                }

                    if($valueArr['dayType'] == 'Saturday' || $valueArr['dayType'] == 'Sunday') {
                        $displayName = $valueArr['dayType']=='Saturday'?'周六':'周日';
                        $findText = $findText.($valueArr['dayType']=='Saturday'?' startDay=6':' startDay=7');
                    ?>
                        <table class="recommend" style="width:100%;text-align: center">
                            <tbody>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_11','flagText' => $displayName,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <td rowspan="4" class="timeSlot"><?php echo $displayName;?></td>
                                <?php if($valueArr['courtType']['11人制足球场地'] != 0) {?>
                                <td class="court_type">11人制足球场地</td>
                                <td class="leave_count">
                                        剩余场地：<font><?php echo $valueArr['courtType']['11人制足球场地']?></font>
                                    </td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_7','flagText' => $displayName,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['7人制足球场地'] != 0) {?>
                                <td class="court_type">7人制足球场地</td>
                                <td class="leave_count">
                                        剩余场地：<font><?php echo $valueArr['courtType']['7人制足球场地']?></font>
                                    </td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_5','flagText' => $displayName,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['5人制足球场地'] != 0) {?>
                                <td class="court_type">5人制足球场地</td>
                                <td class="leave_count"><a href="">
                                    剩余场地：<font><?php echo $valueArr['courtType']['5人制足球场地']?></font>
                                    </a></td>
                                <?php }?>
                            </tr>
                            <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=badminton_court','flagText' => $displayName,'nowCity' => $city));?>
                            <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                                <?php if($valueArr['courtType']['羽毛球场地'] != 0) {?>
                                <td class="court_type">羽毛球场地</td>
                                <td class="leave_count"><a href="">
                                    剩余场地：<font><?php echo $valueArr['courtType']['羽毛球场地']?></font>
                                    </a></td>
                                <?php }?>
                            </tr>
                            </tbody>
                        </table>
                <?php 
                }                              
            }
            ?>   
        </dd>
    </dl>        
    <footer>
        <div class="footer-copyright">
            <div class="hr"></div>
            <span class="footer-copyright-text">
                Copyright © 2014 billyn.net </span>
        </div>
    </footer>